
var currency = '-';
var exchange = 1.0;
var exchange_item = 1.0;
var grid;

$(function() {
	var columns = [{
	    id: "#",
	    name: "",
	    width: 40,
	    behavior: "selectAndMove",
	    unselectable: true,
	    resizable: false,
	    cssClass: "cell-reorder"
	}, {
	    id: "name",
	    name: "事由",
	    field: "name",
	    width: 150,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text,
	    validator: requiredFieldValidator
	}, {
	    id: "unit",
	    name: "单位",
	    field: "unit",
	    width: 50,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text,
	    validator: requiredFieldValidator
	}, {
	    id: "price",
	    name: "单价",
	    field: "price",
	    width: 100,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text,
	    validator: requiredFieldValidator
	}, {
	    id: "quantity",
	    name: "数量",
	    field: "quantity",
	    width: 80,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text,
	    validator: requiredFieldValidator
	}, {
	    id: "amount",
	    name: "金额",
	    field: "amount",
	    width: 80,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text,
	    validator: requiredFieldValidator
	}, {
	    id: "mold",
	    name: "产品",
	    field: "mold_x",
	    width: 150,
	    cssClass: "cell-title",
	    editor: MoldCellEditor
	}, {
	    id: "content",
	    name: "备注",
	    field: "content",
	    width: 160,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text
	}];

	var options = {
			editable: true,
		    enableAddRow: true,
		    enableCellNavigation: true,
		    asyncEditorLoading: false,
		    rowHeight: 30
		};
		
	grid = new Slick.Grid($("#myGrid"), data, columns, options);
	
	grid.onAddNewRow.subscribe(function (e, args) {
        var item = {unit:"套", quantity:"1.00", price:"1.00", ok: true};
    	
        item.amount = fxmul(item.price, item.quantity);
        
        $.extend(item, args.item);
        data.push(item);
        grid.invalidateRows([data.length - 1]);
        grid.updateRowCount();
        grid.render();
        
        _amount();
    });
    
	grid.onCellChange.subscribe(function(e, args) {
		item = args.item;
		currentCell = args.cell;
		currentRow = args.row;
    	
    	item.amount = fxmul(item.price, item.quantity);
		
    	grid.updateRow(currentRow);
    	grid.updateRowCount();
        grid.render();
    	
        _amount();
    });
});	
